<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="closeReqRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="需求关闭类型" prop="closed_type">
            <y-select
              v-model="form.closed_type"
              placeholder="请选择需求关闭类型"
              :options="pms_req_close"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="关闭说明" prop="closed_remark">
            <el-input
              v-model="form.closed_remark"
              type="textarea"
              :rows="5"
              placeholder="请输入关闭说明"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="ReqClose">
const { proxy } = getCurrentInstance();
const { pms_req_close } = proxy.useDict("pms_req_close");

const emit = defineEmits(["submited", "close"]);

const data = reactive({
  form: {},
  rules: {
    closed_type: [
      { required: true, message: "需求关闭类型不能为空", trigger: "blur" },
    ],
  },
});

const { form, rules } = toRefs(data);

const props = defineProps({
  id: { type: String, default: "" },
  getMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 打开对话框事件 */
function open() {
  props.getMethod(props.id).then((res) => {
    form.value = res.data;
  });
}

/** 重置Form方法 */
function reset() {
  form.value.close = "draft";
  form.value.closed_remark = "";
}

/** 提交按钮事件 */
function submit() {
  proxy.$refs["closeReqRef"].validate((valid) => {
    if (valid) {
      // 关闭需求后，将需求状态置为 “已关闭”
      form.value.status = "closed";
      props.updateMethod(form.value).then((res) => {
        proxy.$modal.msgSuccess("关闭成功");
        submited();
      });
    }
  });
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
